<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="auth" uri="http://www.jiyinit.com/authTag"%>

<div class="right-container">
    <div class="manage-button text-left">
        <auth:auth authUrl="deploy/deployProcess">
            <button id="deployBtn" class="btn btn-primary" type="button">
                <i class="fa fa-gear"></i> 流程部署
            </button>
        </auth:auth>
        <auth:auth authUrl="deploy/deleteProcess">
            <button id="deleteBtn" class="btn btn-primary" type="button">
                <i class="fa fa-trash-o"></i> 删除
            </button>
        </auth:auth>

    </div>

    <div class="row">
        <div class="col-xs-12">
            <table id="grid-table"></table>
            <div id="grid-pager"></div>
        </div>
    </div>
</div>

<!-- page specific plugin scripts -->
<script src="${basePath}/plugins/jqGrid/jquery.jqGrid.min.js${VERSION}"></script>
<script src="${basePath}/plugins/jqGrid/grid.locale-cn.js${VERSION}"></script>

<!-- inline scripts related to this page -->
<script type="text/javascript">
    var pageDialog = null;
    var imgUrl = null;
    jQuery(function ($) {
        CommonUtils.grid({
            url: "${basePath}/deploy/getProcessList",
            colNames: ['主键id', '流程id', '流程定义名称', '版本号', '流程文件名称', '流程图', '流程key'],
            colModel: [
                {name: 'id', index: 'id', key: true, width: 5, sorttype: "int", hidden: true},
                {name: 'deploymentId', index: 'deploymentId', width: 10},
                {name: 'name', index: 'name', width: 20},
                {name: 'version', index: 'version', width: 10},
                {name: 'resourceName', index: 'resourceName', width: 30},
                {
                    name: 'diagramResourceName', index: 'diagramResourceName', width: 30,
                    formatter: function (cellvalue, options, rowdata) {
                        return "<a href=\"${basePath}/deploy/showProcessImg?pid=" + rowdata.id +
                                "&resource=" + rowdata.diagramResourceName + "\"" +
                                " onclick=\"return showProcessImg(this)\" >" + rowdata.diagramResourceName + "</a>";
                    }
                },
                {name: 'key', index: 'key', width: 30},
                /*{name: 'diagramResourceName', index: 'diagramResourceName', width: 30},*/
            ],
            multiselect: true,
            multiboxonly: true,
        });



        $("#deployBtn").on("click", function () {
            pageDialog = layer.open({
                title: "流程部署",
                type: 2,
                maxmin: true,
                area: ["50%", "70%"],
                content: ['${basePath}/deploy/toDeploy'],
                /*end: function () {
                    $("#grid-table").trigger("reloadGrid");
                }*/
            });
        });

        $("#deleteBtn").on("click", function () {
            var rowIds = $('#grid-table').jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length > 0){
                var ids = "";
                $.each(rowIds, function (index, value) {
                    var rowData = $('#grid-table').jqGrid('getRowData', value);
                    if(rowIds.length == (index+1)){
                        ids = ids + rowData.deploymentId;
                    }else{
                        ids = ids + rowData.deploymentId + ",";
                    }
                });
                layer.confirm("确定要删除选中数据?", {icon:3},
                function (index) {
                    deleteProcessFunc(ids);
                },
                function (index) {
                    layer.close(index);
                });
            }else{
                layer.alert("请至少选择一行数据进行删除!", {icon: 0});
            }
        });

        /*删除流程函数*/
        function deleteProcessFunc(ids) {
            //console.log(ids);
            var data = {"ids": ids}
            $.ajax({
                type: "post",
                url: "${basePath}/deploy/deleteProcess",
                cache: false,
                data: data,
                dataType: 'json',
                success: function (data) {
                    if(data.status == '0'){
                        layer.alert("删除流程成功!", {icon: 1}, function (index) {
                            var page = $('#grid-table').getGridParam('page'); //获取当前页码
                            $('#grid-table').jqGrid().trigger("reloadGrid", [{page: page}]);//刷新当前页
                            layer.close(index);
                        });

                    }else if(data.status == '1') {
                        layer.alert("系统异常!", {icon: 0});
                    }
                },
                error: function () {
                    layer.alert("系统异常!", {icon: 0});
                }
            });
        }

    });

    function showProcessImg(obj) {
        var href = $(obj).attr("href");
        imgUrl = href;
        pageDialog = layer.open({
            title: "查看流程",
            type: 2,
            maxmin: true,
            area: ["100%", "100%"],
            content: [ '${basePath}/deploy/toProcessImg' ],
        });
        return false;
    }
</script>
